package com.binguo.api.yprecapi;

import com.binguo.api.baidu.BaiduRecUtil;
import com.binguo.api.baidu.model.*;
import com.binguo.api.businessmodel.ProductImgDetailModel;
import com.binguo.api.businessmodel.ProductImgModel;
import com.binguo.api.form.ProductCorrectForm;
import com.binguo.api.form.ProductCorrectItemForm;
import com.binguo.api.huawei.imagesearch.model.HuaweiProduct;
import com.binguo.api.huawei.imagesearch.model.RecResult;
import com.binguo.api.imgrec.ImgRecResult;
import com.binguo.api.imgrec.Results;
import com.binguo.api.response.product.ProductRecOthersVO;
import com.binguo.api.response.product.ProductRecResultDetailVONewWithLocation;
import com.binguo.api.response.product.ProductRecResultVONewWithLocation;
import com.binguo.api.schedule.DataLoadService;
import com.binguo.common.paymodel.ThreadManager;
import com.binguo.common.util.ImageBase64Util;
import com.binguo.common.util.ImageUtil;
import com.binguo.common.util.JsonUtil;
import com.binguo.common.util.LoggerUtil;
import com.binguo.model.mapper.BranchMapper;
import com.binguo.model.mapper.LogProductRecMapper;
import com.binguo.model.mapper.ProductMapper;
import com.binguo.model.model.*;
import com.binguo.util.IOUUtil;
import com.binguo.yprecapi.RecDataDetail;
import com.binguo.yprecapi.RecDataResultVO;
import com.binguo.yprecapi.YpRecService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.stream.Collectors;


@Service
public class YpRecUtilWithPlateAndNoCutService {

    private static Logger logger = LoggerFactory.getLogger(YpRecUtilWithPlateAndNoCutService.class);

    @Autowired
    YpRecService ypRecService;

    @Autowired
    BaiduRecUtil baiduRecUtil;

    @Resource
    private LogProductRecMapper logProductRecMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private BranchMapper branchMapper;

    public Product noneRecProduct = new Product();

    public String recIp;

    public String ipAddress;
    public String localIpAddress;
    public String imgPath = "/usr/local/nginx/html";

    private List<Integer> existedShopIdList = new ArrayList<>();
    public HttpHeaders DEFAULT_JSON_HEADER;

    public HttpHeaders DEFAULT_JSON_HEADER_UTF8;

    public HttpHeaders headers = new HttpHeaders();
    public RestTemplate restTemplate = new RestTemplate();


    public YpRecUtilWithPlateAndNoCutService() {
        DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.APPLICATION_FORM_URLENCODED;
        DEFAULT_JSON_HEADER.setContentType(jsontype);


        DEFAULT_JSON_HEADER_UTF8 = new HttpHeaders();
        DEFAULT_JSON_HEADER_UTF8.setContentType(MediaType.APPLICATION_JSON_UTF8);

        noneRecProduct.setName("未识别菜品");
        noneRecProduct.setId(1);
        noneRecProduct.setPrice(1l);

        recIp = System.getenv("recIp");

        if (StringUtils.isEmpty(recIp)) {
            recIp = "172.17.0.5";//newbinguo
        }

        this.ipAddress = System.getenv().get("ipAddress");
        this.localIpAddress = System.getenv().get("localIpAddress");

        if (this.ipAddress == null) {
            this.ipAddress = "";
        }

        headers.add("Content-Type", "application/x-www-form-urlencoded");
    }

    /**
     * 餐盘格子识别
     *
     * @param shopId
     * @param fileBase64Content
     * @param filename
     * @param outputFilepath
     * @param uuidFileName
     * @param imgUrl
     * @param sourceImgOnInternet
     * @param branchId
     * @param recType
     * @return
     * @throws Exception
     */
    public ProductRecResultVONewWithLocation searchWithPalletCell(Integer shopId, String fileBase64Content,
                                                                  String filename, String outputFilepath,
                                                                  String uuidFileName,
                                                                  String imgUrl, String sourceImgOnInternet, Integer branchId, Integer recType
    ) throws Exception {

        Date date = Calendar.getInstance().getTime();
        long startTime = date.getTime();
        logger.info("云澎菜品识别，开始识别:" + startTime);

        List<ProductYpRef> productYpRefList = null;
        if (DataLoadService.shopYpProductListMap != null) {
            productYpRefList = DataLoadService.shopYpProductListMap.get(shopId);
        }

        int width = 80;

        RecResult huaRecResult = getBowlResult(shopId, branchId, fileBase64Content, uuidFileName);

        Future<RecResult> huaRecResultFuture = getProductYp(productYpRefList, shopId, fileBase64Content, uuidFileName, branchId);

        long errorExistStart = System.currentTimeMillis();

        ProductImgModel productImgModel = new ProductImgModel();
        productImgModel.setProductOrderNo(uuidFileName);
        productImgModel.setShopId(shopId);
        productImgModel.setSourceFilename(filename);
        productImgModel.setUuidFileName(uuidFileName);
        productImgModel.setImgUrl(imgUrl);
        productImgModel.setCurIpAddress(ipAddress);
        productImgModel.setSourceImgOnInternet(sourceImgOnInternet);
        productImgModel.setItemList(new ArrayList<>());

        final List<ImgTag> imgTagList = new ArrayList<>();

        ProductRecResultVONewWithLocation productRecResultVO = new ProductRecResultVONewWithLocation();
        productRecResultVO.setProductList(new ArrayList<>());
        productRecResultVO.setTotalFee(0l);
        productRecResultVO.setRealFee(0l);
//        if (huaRecResult != null) {
        int index = 0;
        int seq = 1;


        List<Result> searchResultList = new ArrayList<>();

        if (huaRecResult != null) {
            for (int i = 0; i < huaRecResult.getDetectionClasses().size(); i++) {

                double score = huaRecResult.getDetectionScores().get(i);

//                if (score > 0.5) {
                if (score > DataLoadService.huaweiScore) {
                    List<Double> boxes = huaRecResult.getDetectionBoxes().get(i);

                    if (boxes.get(0) < 10 && boxes.get(1) < 10) {
                        continue;
                    }

                    int y1 = boxes.get(0).intValue();
                    int x1 = boxes.get(1).intValue();

                    int y2 = boxes.get(2).intValue();
                    int x2 = boxes.get(3).intValue();


                    Result temp = new Result();
                    temp.setLocation(new Location());
                    temp.getLocation().setLeft(x1);
                    temp.getLocation().setTop(y1);
                    if (x2 > x1) {
                        temp.getLocation().setWidth(x2 - x1);
                    } else {
                        temp.getLocation().setWidth(x1 - x2);
                    }
                    if (y2 > y1) {
                        temp.getLocation().setHeight(y2 - y1);
                    } else {
                        temp.getLocation().setHeight(y1 - y2);
                    }

                    temp.setDishes(new ArrayList<>());
                    Dishes dishes = new Dishes();
                    dishes.setBrief(huaRecResult.getDetectionClasses().get(i));
                    dishes.setScore(score);
                    temp.getDishes().add(dishes);

                    searchResultList.add(temp);
                }
            }
        }

        List<Result> needRemovedList = null;

        List<Result> iouErrorTempList = null;
        List<Map<String, Result>> iouErrorList = null;

        //searchResultList为筛选和计算座标后的碗
        if (searchResultList.size() > 0) {
            for (Result temp : searchResultList) {
                int x1 = temp.getLocation().getLeft();
                int y1 = temp.getLocation().getTop();

                int x2 = temp.getLocation().getLeft() + temp.getLocation().getWidth();
                int y2 = temp.getLocation().getTop() + temp.getLocation().getHeight();

                //右上角坐标
                int y11 = y1;
                int x11 = x2;

                for (Result compare : searchResultList) {
                    int x3 = compare.getLocation().getLeft();
                    int y3 = compare.getLocation().getTop();

                    int x4 = compare.getLocation().getLeft() + compare.getLocation().getWidth();
                    int y4 = compare.getLocation().getTop() + compare.getLocation().getHeight();

                    //右上角坐标
                    int y22 = y3;
                    int x22 = x4;

                    if (x1 < x3 && y1 < y3
                            && x2 > x4 && y2 > y4
                            ) {
                        logger.info("检测有碗图片包含: top=" + temp.getLocation().getTop());
                        if (needRemovedList == null) {
                            needRemovedList = new ArrayList<>();
                        }
                        needRemovedList.add(temp);
                    }

                    if (((x1 < (x3 + width) && x1 > (x3 - width))
                            && (y1 < (y3 + width) && y1 > (y3 - width)))

                            ||

                            ((x2 < (x4 + width) && x2 > (x4 - width))
                                    && (y2 < (y4 + width) && y2 > (y4 - width)))

                            ||

                            ((x11 < (x22 + width) && x11 > (x22 - width))
                                    && (y11 < (y22 + width) && y11 > (y22 - width)))


                            ) {
                        if (x1 == x3 && y1 == y3 && x2 == x4) {
                            continue;
                        }

                        logger.info("检测碗附近有图片包含: top=" + temp.getLocation().getTop());
                        if (needRemovedList == null) {
                            needRemovedList = new ArrayList<>();
                        }

                        if (temp.getDishes().get(0).getScore() > compare.getDishes().get(0).getScore()) {
                            needRemovedList.add(compare);
                        }
                    }

                    double ratio = IOUUtil.caculateIOU(x1, y1, x2, y2, x3, y3, x4, y4);

                    if (ratio > 0.7) {
                        int tempXWidthLeft = Math.abs(x3 - x1);
                        int tempXWidthRight = Math.abs(x4 - x2);

                        logger.info("检测碗iou比例={}, tempXWidthLeft:{}, tempXWidthRight:{}, temp:{}, compare:{}"
                                , ratio, tempXWidthLeft, tempXWidthRight, temp, compare);

                        boolean overlay = false;
                        //餐盘一般为长方形，长度比宽度长，只比较x轴是否靠近
                        if (ratio > 75) {//超过65%比例
                            if ((tempXWidthLeft < 50 && tempXWidthRight < 100)
                                    || (tempXWidthLeft < 100 && tempXWidthRight < 50)) {
                                overlay = true;
                            }
                        } else {
                            if ((tempXWidthLeft < 40 && tempXWidthRight < 60)
                                    || (tempXWidthLeft < 60 && tempXWidthRight < 40)) {
                                overlay = true;
                            }
                        }

                        if (overlay) {
                            if (iouErrorList == null) {
                                iouErrorList = new ArrayList<>();
                                iouErrorTempList = new ArrayList<>();
                            }

                            if (!iouErrorTempList.contains(temp)) {
                                iouErrorTempList.add(temp);

                                Map<String, Result> iouErrorMap = new HashMap<>();
                                iouErrorMap.put("compare1", temp);
                                iouErrorMap.put("compare2", compare);

                                iouErrorList.add(iouErrorMap);
                            }
                        }

                    }
                }
            }
        }


        List<Future<RecResult>> recFutureList = new ArrayList<>();

        if (searchResultList.size() > 0) {
            for (Result temp : searchResultList) {
                boolean remove = false;
                if (needRemovedList != null) {
                    for (Result badResult : needRemovedList) {
                        if (badResult.getLocation().getTop() == temp.getLocation().getTop()
                                && badResult.getLocation().getWidth() == temp.getLocation().getWidth()
                                && badResult.getLocation().getHeight() == temp.getLocation().getHeight()
                                && badResult.getLocation().getLeft() == temp.getLocation().getLeft()
                                ) {
                            logger.info("跳过被删除的碗: top=" + badResult.getLocation().getTop());
                            remove = true;
                            break;
                        }
                    }
                }

                if (remove) {
                    continue;
                }


                //针对框出碗
                logger.info("======框出碗:top:{}, left:{}, width:{}, height:{}", temp.getLocation().getTop(),
                        temp.getLocation().getLeft(), temp.getLocation().getWidth(), temp.getLocation().getHeight());


                Integer tempSeq = seq;
                Integer tempIndex = index;

                ProductRecResultVONewWithLocation tempProductRecResultVO = productRecResultVO;
                Future<RecResult> localRecFuture = ThreadManager.executorService.submit((Callable) () ->
                {
                    long reRecStart = System.currentTimeMillis();
                    handleNotRecImg(shopId, uuidFileName,
                            temp, tempProductRecResultVO, tempSeq,
                            productImgModel, imgTagList, branchId, tempIndex, recType);
                    long reRecEnd = System.currentTimeMillis();
                    logger.info("======框出后菜品识别时间:{}, tempIndex:{}", reRecEnd - reRecStart, tempIndex);
                    return null;
                });

                recFutureList.add(localRecFuture);

                index++;

                seq++;
            }
            for (Future<RecResult> recResultFuture : recFutureList) {
                recResultFuture.get();
            }
        }

        long errorExistEnd = System.currentTimeMillis();

        logger.info("======框出后菜品识别时间:{}", errorExistEnd - errorExistStart);

        RecResult objectRecResult = null;
        if (huaRecResultFuture != null) {
            objectRecResult = huaRecResultFuture.get();
        }
        long errorExistEnd2 = System.currentTimeMillis();
        logger.info("======云澎菜品库识别时间:{}, 识别结果：{}", errorExistEnd2 - errorExistEnd, objectRecResult);

        List<HuaweiProduct> huaweiProductList = null;
        if (objectRecResult != null) {
            List<String> stringProductYpRefMap = DataLoadService.shopYpProductSearchCodeListMap.get(shopId);

            huaweiProductList = new ArrayList<>();
            for (int i = 0; i < objectRecResult.getDetectionClasses().size(); i++) {
                String detectName = objectRecResult.getDetectionClasses().get(i);
                logger.info("======检测到云澎菜品:{}", detectName);
                if (stringProductYpRefMap != null && stringProductYpRefMap.contains(detectName)) {
                    logger.info("======商户已关联云澎菜品:{}", detectName);
                    double score = objectRecResult.getDetectionScores().get(i);
                    logger.info("======商户已关联云澎菜品，识别分值:{}", score);
                    if (score > DataLoadService.huaweiScore) {
                        List<Double> boxes = objectRecResult.getDetectionBoxes().get(i);

                        if (boxes.get(0) < 10 && boxes.get(1) < 10) {
                            logger.info("======商户已关联云澎菜品，识别分值:{}, 边框大小不正规:{}", score, boxes);
                            continue;
                        }

                        int y1 = boxes.get(0).intValue();
                        int x1 = boxes.get(1).intValue();

                        int y2 = boxes.get(2).intValue();
                        int x2 = boxes.get(3).intValue();

                        //右上角坐标
                        int y11 = y1;
                        int x11 = x2;

                        boolean addFlag = true;

                        boolean needRemove = false;
                        HuaweiProduct needRemoveObj = null;
                        if (huaweiProductList != null && huaweiProductList.size() > 0) {
                            for (HuaweiProduct huaweiProduct : huaweiProductList) {
                                int y3 = huaweiProduct.getLocation().get(0).intValue();
                                int x3 = huaweiProduct.getLocation().get(1).intValue();

                                int y4 = huaweiProduct.getLocation().get(2).intValue();
                                int x4 = huaweiProduct.getLocation().get(3).intValue();

                                //右上角坐标
                                int y22 = y3;
                                int x22 = x4;

                                if (((x1 < (x3 + width) && x1 > (x3 - width))
                                        && (y1 < (y3 + width) && y1 > (y3 - width)))

                                        ||

                                        ((x2 < (x4 + width) && x2 > (x4 - width))
                                                && (y2 < (y4 + width) && y2 > (y4 - width)))

                                        ||

                                        ((x11 < (x22 + width) && x11 > (x22 - width))
                                                && (y11 < (y22 + width) && y11 > (y22 - width)))

                                        ) {

                                    if (score > huaweiProduct.getScore()) {
                                        addFlag = true;
                                        needRemove = true;
                                        needRemoveObj = huaweiProduct;
                                        break;
                                    } else {
                                        addFlag = false;
                                    }
                                }
                            }
                        }
                        if (needRemove) {
                            for (HuaweiProduct huaweiProduct : huaweiProductList) {
                                if (huaweiProduct.getProductId() == needRemoveObj.getProductId()
                                        && huaweiProduct.getScore() == needRemoveObj.getScore()
                                        ) {
                                    huaweiProductList.remove(huaweiProduct);
                                    break;
                                }
                            }
                        }

                        if (addFlag) {
                            ProductYpRef temp = DataLoadService.getProductYpRefBySearchCode(detectName, shopId, branchId);
                            if (temp == null) {
                                logger.info("======商户已关联云澎菜品，检测合规，菜品查询为空异常:{}", detectName);
                                continue;
                            }
                            logger.info("======商户已关联云澎菜品，检测合规，菜品名称:{}", temp.getProductName());
                            HuaweiProduct huaweiProduct = new HuaweiProduct();
                            huaweiProduct.setProductId(temp.getProductId());
                            huaweiProduct.setProductName(temp.getProductName());
                            huaweiProduct.setLocation(boxes);
                            huaweiProduct.setScore(objectRecResult.getDetectionScores().get(i));
                            huaweiProductList.add(huaweiProduct);
                        }
                    }else{
                        logger.info("======商户已关联云澎菜品，删除，分数小于规定范围，:{}", DataLoadService.huaweiScore);
                    }
                } else {
                    logger.info("======商户未关联云澎菜品:{}", detectName);
                }
            }

        }


//            Set<Result> findDuplicatedProductList = getRemoveList(searchResultList, huaweiProductList, width, productRecResultVO.getProductList());
        Set<Result> findDuplicatedProductList = new HashSet<>();
        Set<HuaweiProduct> findDuplicatedProductYpList = new HashSet<>();

        if (searchResultList != null && searchResultList.size() > 0) {
            for (Result temp : searchResultList) {
                logger.info("碗：{}", temp.getLocation());
                int x1 = temp.getLocation().getLeft();
                int y1 = temp.getLocation().getTop();

                int x2 = temp.getLocation().getLeft() + temp.getLocation().getWidth();
                int y2 = temp.getLocation().getTop() + temp.getLocation().getHeight();

                //右上角坐标
                int y11 = y1;
                int x11 = x2;

                String brief = temp.getDishes().get(0).getBrief();

                Integer searchProductId = null;

                int confident = 0;

                for (ProductRecResultDetailVONewWithLocation productRecResultDetailVONewWithLocation : productRecResultVO.getProductList()) {
                    if (
                            productRecResultDetailVONewWithLocation.getX1().intValue() == x1 &&
                                    productRecResultDetailVONewWithLocation.getX2().intValue() == x2 &&
                                    productRecResultDetailVONewWithLocation.getY1().intValue() == y1 &&
                                    productRecResultDetailVONewWithLocation.getY2().intValue() == y2
                            ) {
                        searchProductId = productRecResultDetailVONewWithLocation.getProductId();
                        temp.getDishes().get(0).setBrief(searchProductId.toString());
                        confident = productRecResultDetailVONewWithLocation.getConfident();

                        break;
                    }
                }
                logger.info("碗识别菜品id：{}, confident:{}", searchProductId, confident);

                if (huaweiProductList != null && huaweiProductList.size() > 0) {
                    logger.info("存在云澎菜品库：{}个", huaweiProductList.size());
                    for (HuaweiProduct huaweiProduct : huaweiProductList) {
                        logger.info("云澎菜品库位置：{}", huaweiProduct.getLocation());
                        int y3 = huaweiProduct.getLocation().get(0).intValue();
                        int x3 = huaweiProduct.getLocation().get(1).intValue();

                        int y4 = huaweiProduct.getLocation().get(2).intValue();
                        int x4 = huaweiProduct.getLocation().get(3).intValue();

                        double ratio = IOUUtil.caculateIOU(x1, y1, x2, y2, x3, y3, x4, y4);
                        double ratio2 = IOUUtil.caculateIOU(x3, y3, x4, y4, x1, y1, x2, y2);

                        if (ratio > 0.8 || ratio2 > 0.8) {
                            logger.info("ratio==={}, ratio2==={}", ratio, ratio2);
                            if (confident == 1) {//菜品识别结果分值为1，删除云澎菜品库结果
                                logger.info("菜品识别结果分值为1，删除云澎菜品库结果");
                                findDuplicatedProductYpList.add(huaweiProduct);
                            } else {//菜品识别结果分值为0，删除碗菜品结果
                                logger.info("菜品识别结果分值为0，删除碗菜品结果");
                                findDuplicatedProductList.add(temp);
                            }

                            continue;
                        }
                        /*//右上角坐标
                        int y22 = y3;
                        int x22 = x4;

                        if (x1 < x3 && y1 < y3
                                && x2 > x4 && y2 > y4
                                ) {

                            logger.info("检测有图片包含: top=" + temp.getLocation().getTop() + ",searchProductId=" + searchProductId);
                            logger.info("菜品识别分值:{}: top=" + temp.getLocation().getTop() + ",searchProductId=" + searchProductId);

                            if (confident == 1) {//菜品识别结果分值为1，删除云澎菜品库结果
                                logger.info("菜品识别结果分值为1，删除云澎菜品库结果");
                                findDuplicatedProductYpList.add(huaweiProduct);
                            } else {//菜品识别结果分值为0，删除碗菜品结果
                                logger.info("菜品识别结果分值为0，删除碗菜品结果");
                                findDuplicatedProductList.add(temp);
                            }

                            continue;
                        }

                        if (((x1 < (x3 + width) && x1 > (x3 - width))
                                && (y1 < (y3 + width) && y1 > (y3 - width)))

                                ||

                                ((x2 < (x4 + width) && x2 > (x4 - width))
                                        && (y2 < (y4 + width) && y2 > (y4 - width)))

                                ||

                                ((x11 < (x22 + width) && x11 > (x22 - width))
                                        && (y11 < (y22 + width) && y11 > (y22 - width)))

                                ) {

                            logger.info("检测到云澎菜品库商品,id:" + huaweiProduct.getProductId() +
                                    ",name=" + huaweiProduct.getProductName() + ",baiduTop:" + x3 + ",searchProductId=" + searchProductId);


                            if (confident == 1) {//菜品识别结果分值为1，删除云澎菜品库结果
                                logger.info("菜品识别结果分值为1，删除云澎菜品库结果");
                                findDuplicatedProductYpList.add(huaweiProduct);
                            } else {//菜品识别结果分值为0，删除碗菜品结果
                                logger.info("菜品识别结果分值为0，删除碗菜品结果");
                                findDuplicatedProductList.add(temp);
                            }

                        }*/

                    }
                }
            }
        }

        if (findDuplicatedProductList != null && findDuplicatedProductList.size() > 0 &&
                productRecResultVO.getProductList() != null && productRecResultVO.getProductList().size() > 0) {
            logger.info("检测需删除的碗菜品识别结果size==" + findDuplicatedProductList.size());

            List<ProductRecResultDetailVONewWithLocation> toBeRemovedList = null;
            for (ProductRecResultDetailVONewWithLocation productRecResultDetailVONewWithLocation
                    : productRecResultVO.getProductList()) {

                for (Result result : findDuplicatedProductList) {

                    Integer tobeRemovedProductId = Integer.valueOf(result.getDishes().get(0).getBrief());
                    if (tobeRemovedProductId.equals(productRecResultDetailVONewWithLocation.getProductId())) {
                        int x1 = result.getLocation().getLeft();
                        int y1 = result.getLocation().getTop();

                        int x2 = result.getLocation().getLeft() + result.getLocation().getWidth();
                        int y2 = result.getLocation().getTop() + result.getLocation().getHeight();

                        if (x1 == productRecResultDetailVONewWithLocation.getX1()
                                && x2 == productRecResultDetailVONewWithLocation.getX2()
                                && y1 == productRecResultDetailVONewWithLocation.getY1()
                                && y2 == productRecResultDetailVONewWithLocation.getY2()
                                ) {
                            if (toBeRemovedList == null) {
                                toBeRemovedList = new ArrayList<>();
                            }
                            toBeRemovedList.add(productRecResultDetailVONewWithLocation);
                            break;
                        }
                    }
                }

            }

            logger.info("toBeRemovedList==" + toBeRemovedList);
            if (toBeRemovedList != null) {
                if (imgTagList != null && imgTagList.size() > 0) {
                    List<ImgTag> imgTagToBeRemoveList = new ArrayList<>();
                    for (ImgTag imgTag : imgTagList) {
                        for (ProductRecResultDetailVONewWithLocation productRecResultDetailVONewWithLocation : toBeRemovedList) {

                            if (
                                    productRecResultDetailVONewWithLocation.getX1().intValue() == imgTag.getX1() &&
                                            productRecResultDetailVONewWithLocation.getX2().intValue() == imgTag.getX2() &&
                                            productRecResultDetailVONewWithLocation.getY1().intValue() == imgTag.getY1() &&
                                            productRecResultDetailVONewWithLocation.getY2().intValue() == imgTag.getY2()
                                    ) {
                                imgTagToBeRemoveList.add(imgTag);
                                logger.info("to be remove imgTag==" + JsonUtil.toJson(imgTag));
                            }
                        }
                    }
                    if (imgTagToBeRemoveList.size() > 0) {
                        imgTagList.removeAll(imgTagToBeRemoveList);
                    }
                }
                productRecResultVO.getProductList().removeAll(toBeRemovedList);
                if (productRecResultVO.getProductList() == null) {
                    productRecResultVO.setProductList(new ArrayList<>());
                }
            }


        }



        if (iouErrorList != null && iouErrorList.size() > 0) {
            List<ProductRecResultDetailVONewWithLocation> iouToBeRemovedList = new ArrayList<>();
            for (Map<String, Result> map : iouErrorList) {
                Result r1 = map.get("compare1");
                Result r2 = map.get("compare2");
                int x1 = r1.getLocation().getLeft();
                int y1 = r1.getLocation().getTop();

                int x2 = r1.getLocation().getLeft() + r1.getLocation().getWidth();
                int y2 = r1.getLocation().getTop() + r1.getLocation().getHeight();

                int b_x1 = r2.getLocation().getLeft();
                int b_y1 = r2.getLocation().getTop();

                int b_x2 = r2.getLocation().getLeft() + r2.getLocation().getWidth();
                int b_y2 = r2.getLocation().getTop() + r2.getLocation().getHeight();

                int area1 = r1.getLocation().getWidth() * r1.getLocation().getHeight();
                int area2 = r2.getLocation().getWidth() * r2.getLocation().getHeight();
                int confident1 = 0;
                int confident2 = 0;

                ProductRecResultDetailVONewWithLocation p1 = null;
                ProductRecResultDetailVONewWithLocation p2 = null;
                for (ProductRecResultDetailVONewWithLocation productRecResultDetailVONewWithLocation : productRecResultVO.getProductList()) {
                    if (
                            productRecResultDetailVONewWithLocation.getX1().intValue() == x1 &&
                                    productRecResultDetailVONewWithLocation.getX2().intValue() == x2 &&
                                    productRecResultDetailVONewWithLocation.getY1().intValue() == y1 &&
                                    productRecResultDetailVONewWithLocation.getY2().intValue() == y2
                            ) {
                        confident1 = productRecResultDetailVONewWithLocation.getConfident().intValue();
                        p1 = productRecResultDetailVONewWithLocation;
                    }
                    if (
                            productRecResultDetailVONewWithLocation.getX1().intValue() == b_x1 &&
                                    productRecResultDetailVONewWithLocation.getX2().intValue() == b_x2 &&
                                    productRecResultDetailVONewWithLocation.getY1().intValue() == b_y1 &&
                                    productRecResultDetailVONewWithLocation.getY2().intValue() == b_y2
                            ) {
                        confident2 = productRecResultDetailVONewWithLocation.getConfident().intValue();
                        p2 = productRecResultDetailVONewWithLocation;
                    }
                }

                if (confident1 > confident2) {
                    logger.info("confident1 > confident2, result1=={}, result2=={}" , r1, r2);
                    if (p2 != null) {
                        iouToBeRemovedList.add(p2);
                    }
                }else {
                    if (confident1 == confident2) {
                        logger.info("confident1 == confident2, result1=={}, result2=={}" , r1, r2);
                        if (area1 > area2) {
                            if (p1 != null) {
                                iouToBeRemovedList.add(p1);
                            }
                        }else {
                            if (p2 != null) {
                                iouToBeRemovedList.add(p2);
                            }
                        }
                    }else{
                        logger.info("confident1 < confident2, result1=={}, result2=={}" , r1, r2);
                        if (p1 != null) {
                            iouToBeRemovedList.add(p1);
                        }
                    }
                }

            }

            logger.info("iouToBeRemovedList=={}", iouToBeRemovedList);
            if (iouToBeRemovedList != null && iouToBeRemovedList.size() > 0) {
                if (imgTagList != null && imgTagList.size() > 0) {
                    List<ImgTag> imgTagToBeRemoveList = new ArrayList<>();
                    for (ImgTag imgTag : imgTagList) {
                        for (ProductRecResultDetailVONewWithLocation productRecResultDetailVONewWithLocation : iouToBeRemovedList) {

                            if (
                                    productRecResultDetailVONewWithLocation.getX1().intValue() == imgTag.getX1()&&
                                            productRecResultDetailVONewWithLocation.getX2().intValue() == imgTag.getX2()&&
                                            productRecResultDetailVONewWithLocation.getY1().intValue() == imgTag.getY1()&&
                                            productRecResultDetailVONewWithLocation.getY2().intValue() == imgTag.getY2()
                                    ) {
                                imgTagToBeRemoveList.add(imgTag);
                                logger.info("to be remove imgTag==" + imgTag);
                            }
                        }
                    }
                    if (imgTagToBeRemoveList.size() > 0) {
                        imgTagList.removeAll(imgTagToBeRemoveList);
                    }
                }
                productRecResultVO.getProductList().removeAll(iouToBeRemovedList);
                if (productRecResultVO.getProductList() == null) {
                    productRecResultVO.setProductList(new ArrayList<>());
                }
            }
        }


        if (huaweiProductList != null && huaweiProductList.size() > 0) {
            for (HuaweiProduct huaweiProduct : huaweiProductList) {
                int y1 = huaweiProduct.getLocation().get(0).intValue();
                int x1 = huaweiProduct.getLocation().get(1).intValue();

                int y2 = huaweiProduct.getLocation().get(2).intValue();
                int x2 = huaweiProduct.getLocation().get(3).intValue();


                if (findDuplicatedProductYpList.size() > 0) {
                    logger.info("检测需删除的云澎菜品识别结果size==" + findDuplicatedProductYpList.size());
                    boolean needBreak = false;
                    for (HuaweiProduct temp : findDuplicatedProductYpList) {
                        int tempy1 = temp.getLocation().get(0).intValue();
                        int tempx1 = temp.getLocation().get(1).intValue();

                        int tempy2 = temp.getLocation().get(2).intValue();
                        int tempx2 = temp.getLocation().get(3).intValue();
                        if (x1 == tempx1 && y1 == tempy1 && x2 == tempx2 && y2 == tempy2) {
                            needBreak = true;
                            break;
                        }
                    }

                    if (needBreak) {
                        logger.info("需删除的云澎菜品库菜品==" + huaweiProduct.getLocation());
                        continue;
                    }
                }
                logger.info("添加至结果的云澎菜品库菜品==" + huaweiProduct.getProductName());
                Product product = DataLoadService.productMap.get(huaweiProduct.getProductId());

                if (product == null) {
                    logger.info("产品被意外删除==" + huaweiProduct.getProductName());
                    //产品被意外删除情况判断
                    continue;
                }
                String label = product.getName();


                ImgTag imgTag = new ImgTag();
                imgTag.setX1(x1);
                imgTag.setY1(y1);
                imgTag.setX2(x2);
                imgTag.setY2(y2);
                imgTag.setLabel(label);
                imgTag.setScore(1);
                imgTagList.add(imgTag);


                /**
                 * save ProductImgDetailModel
                 */
                ProductImgDetailModel productImgDetailModel = new ProductImgDetailModel();
                productImgDetailModel.setProductName(product.getName());
                productImgDetailModel.setProductId(product.getId());
                productImgDetailModel.setSeq(seq);
                productImgDetailModel.setLeft(x1);
                productImgDetailModel.setTop(y1);
                if (x2 > x1) {
                    productImgDetailModel.setWidth(x2 - x1);
                } else {
                    productImgDetailModel.setWidth(x1 - x2);
                }

                if (y2 > y1) {
                    productImgDetailModel.setHeight(y2 - y1);
                } else {
                    productImgDetailModel.setHeight(y1 - y2);
                }
                productImgDetailModel.setScore(1);

                productImgModel.getItemList().add(productImgDetailModel);
                ///----end

                ProductRecResultDetailVONewWithLocation productRecResultDetailVO
                        = new ProductRecResultDetailVONewWithLocation();

                productRecResultDetailVO.setX1(x1);
                productRecResultDetailVO.setX2(x2);
                productRecResultDetailVO.setY1(y1);
                productRecResultDetailVO.setY2(y2);
                productRecResultDetailVO.setConfident(1);

                productRecResultDetailVO.setProductName(product.getName());
                productRecResultDetailVO.setProductId(product.getId());

                productRecResultDetailVO.setProductPrice(product.getPrice());
                productRecResultDetailVO.setItemCount(1);
                productRecResultDetailVO.setSeq(seq);

                productRecResultVO.getProductList().add(productRecResultDetailVO);
                productRecResultVO.setTotalFee(productRecResultVO.getTotalFee().longValue()
                        + product.getPrice().longValue());

                seq++;
            }
        }





        try {

            if ((searchResultList != null && searchResultList.size() > 0)
                    || huaRecResult != null
                    ) {

                seq = 1;
                long totalFee = 0;
                for (ProductRecResultDetailVONewWithLocation p : productRecResultVO.getProductList()) {
                    p.setSeq(seq);
                    totalFee += p.getProductPrice();


                    for (ImgTag imgTag : imgTagList) {
                        if (p.getX1() == imgTag.getX1()
                                && p.getX2() == imgTag.getX2()
                                && p.getY1() == imgTag.getY1()
                                && p.getY2() == imgTag.getY2()
                                ) {
                            imgTag.setLabel(seq + "、" + imgTag.getLabel());
                        }
                    }
                    seq++;
                }
                productRecResultVO.setRealFee(totalFee);
                productRecResultVO.setTotalFee(totalFee);
                productRecResultVO.setProductCount(productRecResultVO.getProductList().size());

                ThreadManager.executorService.submit(() -> {
                    try {
                        if (productImgModel.getItemList() != null && productImgModel.getItemList().size() > 0) {
                            for (ProductImgDetailModel productImgDetailModel: productImgModel.getItemList()) {

                                int modelTop = productImgDetailModel.getTop();
                                int modelHeight = productImgDetailModel.getHeight();
                                int modelLeft = productImgDetailModel.getLeft();
                                int modelWidth = productImgDetailModel.getWidth();
                                for (ImgTag imgTag : imgTagList) {
                                    if (modelLeft == imgTag.getX1()
                                            && modelTop == imgTag.getY1()
                                            ) {
                                        Integer modelSeq = Integer.valueOf(imgTag.getLabel().split("、")[0]);
                                        productImgDetailModel.setSeq(modelSeq);
                                    }
                                }

                            }
                        }
                        baiduRecUtil.setKey(productImgModel.getProductOrderNo(), productImgModel, 60 * 60 * 1000);
                        ProductImgJsonInfo productImgJsonInfo = new ProductImgJsonInfo();
                        productImgJsonInfo.setProductContent(JsonUtil.toJson(productImgModel));
                        productImgJsonInfo.setProductOrderNo(productImgModel.getProductOrderNo());

                        baiduRecUtil.productImgJsonInfoMapper.insert(productImgJsonInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }

            if (productRecResultVO == null) {
                return null;
            }

            String imgJson = JsonUtil.toJson(imgTagList);

            logger.info("imgJson==" + imgJson);
            String temp = Base64.getEncoder().encodeToString(imgJson.getBytes());
            logger.info(temp);

            imgJson = URLEncoder.encode(temp);
            logger.info(imgJson);

            long endTime2 = System.currentTimeMillis();
            logger.info("endTime3=" + endTime2 + ", 总时间=" + (endTime2 - startTime));

            String tempOutputFilepath = outputFilepath.replaceAll(imgPath, "/data/html");
            String tempFilename = filename.replaceAll(imgPath, "/data/html");

            String drwaImgParam = "outputFilepath=" + tempOutputFilepath + "&filename=" + tempFilename + "&image=" + imgJson;

            HttpEntity drawEntity = new HttpEntity<>(drwaImgParam, baiduRecUtil.DEFAULT_JSON_HEADER);

            ResponseEntity<String> drwaImgResult = baiduRecUtil.restTemplate.postForEntity("http://" + this.localIpAddress +":5000/hello2", drawEntity, String.class);

            logger.info(drwaImgResult.getBody());


            long endTime3 = System.currentTimeMillis();
            logger.info("endTime4=" + endTime3 + ",画框时间 intelval=" + (endTime3 - endTime2));

            if ("ok".equals(drwaImgResult.getBody())) {

                ThreadManager.executorService.submit(() -> {
                    LogProductRec logProductRec = new LogProductRec();
                    logProductRec.setFilepath(filename);
                    logProductRec.setOutputFilepath(outputFilepath);
                    logProductRec.setCreateTime(date);
                    logProductRec.setShopId(shopId);
                    logProductRec.setImgSrcNetUrl(sourceImgOnInternet);
                    logProductRec.setRecResult(null);
                    logProductRec.setImgResultNetUrl(imgUrl);
                    logProductRecMapper.insert(logProductRec);
                });

                ShopPackageDiscount shopPackageDiscount = baiduRecUtil.checkProductPackage(shopId, productRecResultVO);

                if (shopPackageDiscount != null) {
                    productRecResultVO.setRealFee(shopPackageDiscount.getPrice());
                    productRecResultVO.setMealSetName(shopPackageDiscount.getName());
                    productRecResultVO.setMealSetId(shopPackageDiscount.getId());
                }

                return productRecResultVO;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private RecResult getBowlResult(Integer shopId, Integer branchId,
                                    String fileBase64Content, String uuidFileName) throws Exception{
        long localrecStart = System.currentTimeMillis();


        String url = "http://"+this.localIpAddress+":8080/predict";

        Map<String, String> param = new HashMap<>();
        param.put("filecontent", fileBase64Content);
        param.put("shopid", branchId.toString());
        param.put("uuidFileName", uuidFileName);
        String jsonParam = JsonUtil.toJson(param);

        HttpEntity entity = new HttpEntity<>(jsonParam, baiduRecUtil.DEFAULT_JSON_HEADER_UTF8);

//        logger.info("bowl rec ip = {}, content={}", url, jsonParam);

        ResponseEntity<String> recImgResult = restTemplate.postForEntity(url, entity, String.class);

        try {
            String resultStr = recImgResult.getBody();

            logger.info("碗识别结果：{}", resultStr);
            if (resultStr == null) {
                logger.info("碗识别无结果=");
                return null;
            }
            resultStr = resultStr.substring(resultStr.indexOf("results") - 2);
            ImgRecResult resResult = JsonUtil.parse(resultStr, ImgRecResult.class);


            logger.info("碗识别结果数量：{}", resResult.getNumber());

            if (resResult.getNumber() > 0) {

                RecResult huaRecResult = new RecResult();
                huaRecResult.setDetectionBoxes(new ArrayList<>());
                huaRecResult.setDetectionClasses(new ArrayList<>());
                huaRecResult.setDetectionScores(new ArrayList<>());

                for (Results res : resResult.getResults()) {
                    int x1 = res.getLocation().getLeft();
                    int y1 = res.getLocation().getTop();

                    int x2 = res.getLocation().getLeft() + res.getLocation().getWidth();
                    int y2 = res.getLocation().getTop() + res.getLocation().getHeight();

                    logger.info("碗识别分数：" + res.getScore());
                    if (res.getScore() < 0.9) {
                        logger.info("碗识别分数过小，结果=" + JsonUtil.toJson(res) + ",分数：" + res.getScore());
                        continue;
                    }
                    if (DataLoadService.shopBowlMinAreaMap != null) {
                        Integer minArea = DataLoadService.shopBowlMinAreaMap.get(shopId);
                        if (minArea != null) {
                            long area = res.getLocation().getWidth() * res.getLocation().getHeight();
                            if (area < minArea) {
                                logger.info("碗识别面积过小，结果=" + JsonUtil.toJson(res) + ",面积：" + area);
                                continue;
                            }
                        }
                    }
                    if (DataLoadService.shopBowlMaxAreaMap != null) {
                        Integer maxArea = DataLoadService.shopBowlMaxAreaMap.get(shopId);
                        if (maxArea != null) {
                            long area = res.getLocation().getWidth() * res.getLocation().getHeight();
                            if (area > maxArea) {
                                logger.info("碗识别面积过大，结果=" + JsonUtil.toJson(res) + ",面积：" + area);
                                continue;
                            }
                        }
                    }

                    List<Double> location = new ArrayList<>();
                    location.add(Double.valueOf(y1));
                    location.add(Double.valueOf(x1));
                    location.add(Double.valueOf(y2));
                    location.add(Double.valueOf(x2));

                    huaRecResult.getDetectionBoxes().add(location);
                    huaRecResult.getDetectionScores().add(res.getScore());
                    huaRecResult.getDetectionClasses().add(res.getName());
                }


                long endTime = System.currentTimeMillis();
                logger.info("碗识别结果=" + JsonUtil.toJson(huaRecResult) + " , 筛选后数量:" + huaRecResult.getDetectionClasses().size());
                logger.info("碗识别完成=" + endTime + ",huawei rec intelval=" + (endTime - localrecStart));


                return huaRecResult;
            } else {
                long endTime = System.currentTimeMillis();
                logger.info("本地模型识别无结果");
                logger.info("本地模型识别完成=" + endTime + ",huawei rec intelval=" + (endTime - localrecStart));
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("碗识别错误：{}", e.getMessage());
            return null;
        }
    }


    private Future<RecResult> getProductYp(List<ProductYpRef> productYpRefList, Integer shopId, String fileBase64Content, String uuidFileName, Integer branchId) {
        if (productYpRefList != null && productYpRefList.size() > 0) {
            logger.info("包含云澎菜品库商品: shopId:" + shopId);

            String url = DataLoadService.shopLocalRecUrlmap.get(shopId);

            Map<String, ProductYp> productYpSearchCodeMap = DataLoadService.shopProductYpSearchCodeMap.get(shopId);
            logger.info("url====={}", url);
            Future<RecResult> localRecFuture = ThreadManager.executorService.submit((Callable) () ->
                    {
                        long localrecStart = System.currentTimeMillis();


                        Map<String, String> param = new HashMap<>();
                        param.put("filecontent", fileBase64Content);
                        param.put("shopid", branchId.toString());
                        param.put("uuidFileName", uuidFileName);
                        String jsonParam = JsonUtil.toJson(param);

                        HttpEntity entity = new HttpEntity<>(jsonParam, baiduRecUtil.DEFAULT_JSON_HEADER_UTF8);

//                        logger.info("jsonParam====={}", jsonParam);

                        ResponseEntity<String> recImgResult = restTemplate.postForEntity(url, entity, String.class);
//
//                        Map<String, String> param = new HashMap<>();
//                        param.put("shopId", shopId + "");
//                        param.put("imgUrl", sourceImgOnInternet);
//                        String jsonParam = JsonUtil.toJson(param);
//
//                        HttpEntity entity = new HttpEntity<>(jsonParam, baiduRecUtil.DEFAULT_JSON_HEADER_UTF8);
//
//
//                        ResponseEntity<String> recImgResult = new RestTemplate().postForEntity(url, entity, String.class);

                        try {
                            String resultStr = recImgResult.getBody();

                            System.out.println(resultStr);
                            logger.info("云澎菜品库识别结果：{}", resultStr);
                            if (resultStr == null) {
                                logger.info("云澎菜品库识别无结果=");
                                return null;
                            }
                            resultStr = resultStr.substring(resultStr.indexOf("results") - 2);
                            ImgRecResult resResult = JsonUtil.parse(resultStr, ImgRecResult.class);


                            logger.info("云澎菜品库识别结果数量：{}", resResult.getNumber());

                            double scoreStandard = DataLoadService.huaweiScore;

                            if (DataLoadService.shopLocalRecScoreMap != null) {
                                Double tempScore = DataLoadService.shopLocalRecScoreMap.get(shopId);
                                if (tempScore != null) {
                                    scoreStandard = tempScore;
                                }
                            }
                            logger.info("云澎菜品库分值阈值：{}", scoreStandard);

                            if (resResult.getNumber() > 0) {

                                RecResult result = new RecResult();
                                result.setDetectionBoxes(new ArrayList<>());
                                result.setDetectionClasses(new ArrayList<>());
                                result.setDetectionScores(new ArrayList<>());

                                for (Results res : resResult.getResults()) {
                                    int x1 = res.getLocation().getLeft();
                                    int y1 = res.getLocation().getTop();

                                    int x2 = res.getLocation().getLeft() + res.getLocation().getWidth();
                                    int y2 = res.getLocation().getTop() + res.getLocation().getHeight();


                                    if (res.getScore() < scoreStandard) {
                                        logger.info("云澎菜品库分值小于阈值：{},{}", res.getScore(), scoreStandard);
                                        continue;
                                    }
                                    ProductYp productYp = productYpSearchCodeMap.get(res.getName());

                                    logger.info("云澎菜品库搜索菜品：{},{}", res.getName(), productYp);
                                    if (productYp != null) {
                                        long area = res.getLocation().getWidth() * res.getLocation().getHeight();
                                        if (productYp.getMinArea() != null) {
                                            if (area < productYp.getMinArea().intValue()) {
                                                logger.info("本地模型识别面积过小==" + JsonUtil.toJson(res));
                                                continue;
                                            }
                                        }
                                        if (productYp.getMaxArea() != null) {
                                            if (area > productYp.getMaxArea().intValue()) {
                                                logger.info("本地模型识别面积过大==" + JsonUtil.toJson(res));
                                                continue;
                                            }
                                        }
                                    }
                                    List<Double> location = new ArrayList<>();
                                    location.add(Double.valueOf(y1));
                                    location.add(Double.valueOf(x1));
                                    location.add(Double.valueOf(y2));
                                    location.add(Double.valueOf(x2));

                                    result.getDetectionBoxes().add(location);
                                    result.getDetectionScores().add(res.getScore());
                                    result.getDetectionClasses().add(res.getName());
                                }


                                long endTime = System.currentTimeMillis();
                                logger.info("云澎菜品库模型识别结果=" + JsonUtil.toJson(result));
                                logger.info("云澎菜品库模型识别完成=" + endTime + ",huawei rec intelval=" + (endTime - localrecStart));


                                return result;
                            } else {
                                long endTime = System.currentTimeMillis();
                                logger.info("云澎菜品库模型识别无结果");
                                logger.info("云澎菜品库模型识别完成=" + endTime + ",huawei rec intelval=" + (endTime - localrecStart));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.info("云澎菜品库模型识别错误：{}", e.getMessage());
                        }
                        return null;
                    }
            );
            return localRecFuture;

        }

        return null;
    }




    public Integer handleNotRecImg(Integer shopId
            , String uuidFileName, Result temp, ProductRecResultVONewWithLocation productRecResultVO, Integer seq
            ,ProductImgModel productImgModel, List<ImgTag> imgTagList, Integer branchId
            , Integer index, Integer recType
    ){
        long reRecStart = System.currentTimeMillis();

        String localFilepath = imgPath + "/cutimg/" + branchId;

        if (!existedShopIdList.contains(branchId)) {
            File file = new File(localFilepath);
            if (!file.exists()) {
                file.mkdir();
            }
            existedShopIdList.add(branchId);
            localFilepath +=  "/0000";
            file = new File(localFilepath);
            if (!file.exists()) {
                file.mkdir();
            }

            localFilepath += "/temp";
            File imgCutFile = new File(localFilepath);
            if (!imgCutFile.exists()) {
                imgCutFile.mkdir();
            }
        }else {
            localFilepath +=  "/0000";
            localFilepath += "/temp";
        }

        String cutImgPath = localFilepath + "/" + uuidFileName + "_cut" + index + ".png";

        String cutImgUrl = cutImgPath.replace(imgPath, "http://" + recIp + ":8019");

        long reRecEnd1 = System.currentTimeMillis();
        logger.info("========cutImgPath:{}=======时间{}==" ,cutImgPath, reRecEnd1-reRecStart);

        logger.info("===============cutImgPath==" + cutImgPath);


        Integer tempBranchId = getBranchIdByShopId(shopId, branchId, null);

        RecDataResultVO recDataResultVO = null;
        logger.info("===============DataLoadService.recip==" + this.localIpAddress + ":5001");
        recDataResultVO = ypRecService.predictNew(uuidFileName + "_cut" + index,
                temp.getLocation().getLeft() + 10,temp.getLocation().getLeft() + temp.getLocation().getWidth() - 10,
                temp.getLocation().getTop() + 10,temp.getLocation().getTop() + temp.getLocation().getHeight() - 10,
                uuidFileName, tempBranchId, this.localIpAddress + ":5001");


        long reRecEnd2 = System.currentTimeMillis();
        logger.info("========cutImgPath:{}===识别时间{}====切图时间+识别时间{}==" , cutImgPath, reRecEnd2-reRecEnd1, reRecEnd2-reRecStart);
        try {

            logger.info("==cutImgUrl:{}===rec result===={}" , cutImgUrl, JsonUtil.toJson(recDataResultVO));

            BaiduSearchResult baiduSearchResult = null;
            if (recDataResultVO.getError() != 0) {
                if (recType == 1) {
                    baiduSearchResult = new BaiduSearchResult();
                    Result tempResult = new Result();
                    tempResult.setDishes(new ArrayList<>());
                    //识别出错时，标记为未识别菜品
                    Dishes dishes = new Dishes();
                    dishes.setOriginScore(0.1);
                    dishes.setBrief(shopId + "_" + "1");//1为未识别菜品
                    dishes.setScore(0.1);
                    tempResult.getDishes().add(dishes);
                    baiduSearchResult.setResult(new ArrayList<>());
                    baiduSearchResult.getResult().add(tempResult);
                }else {
                    return null;
                }
            }else{
                baiduSearchResult = new BaiduSearchResult();
                baiduSearchResult.setResult(new ArrayList<>());

                for (RecDataDetail recDataDetail : recDataResultVO.getData()) {
                    Result result = new Result();
                    result.setDishes(new ArrayList<>());
                    Dishes dishes = new Dishes();
                    dishes.setScore(Double.valueOf(recDataDetail.getC()));
                    String name = recDataDetail.getLabel();
                    //name格式 shopId_productId_time.png
                    String label = null;
                    if (name.contains("_")) {
                        label = name.substring(0, name.lastIndexOf("_"));
//                        label = name;
                    }else {
                        label = shopId + "_" + name;
                    }

                    dishes.setBrief(label);
                    result.getDishes().add(dishes);

                    baiduSearchResult.getResult().add(result);

                    logger.info("cutImgUrl:{},result.getDishes().add(dishes)={}", cutImgUrl, JsonUtil.toJson(result));
                }
            }

            if (baiduSearchResult != null) {
                List<Result> searchResultList = baiduSearchResult.getResult();

                if (searchResultList != null && searchResultList.size() > 0) {
                    Result recResult = searchResultList.get(0);
                    List<Dishes> dishesList = recResult.getDishes();
                    logger.info("cutImgUrl:{},recResult()={}, dishesList={}", cutImgUrl, recResult, JsonUtil.toJson(dishesList));
                    if (dishesList == null || dishesList.size() == 0) {
                        dishesList = new ArrayList<>();
                        if ((DataLoadService.shopRecOpenMap != null && DataLoadService.shopRecOpenMap.get(shopId) != null &&
                                DataLoadService.shopRecOpenMap.get(shopId).equals(1))) {
                            //纠正时候如有未识别的菜品，标记为未识别菜品
                            Dishes dishes = new Dishes();
                            dishes.setBrief(shopId + "_" + "1");//1为未识别菜品
                            dishes.setOriginScore(0.1);
                            dishes.setScore(0.1);
                            dishesList.add(dishes);
                        }else
                        if (recType == 1) {
                            //纠正时候如有未识别的菜品，标记为未识别菜品
                            Dishes dishes = new Dishes();
                            dishes.setBrief(shopId + "_" + "1");//1为未识别菜品
                            dishes.setOriginScore(0.1);
                            dishes.setScore(0.1);
                            dishesList.add(dishes);
                        }
                    }
                    if (dishesList != null && dishesList.size() > 0) {
                        double score = 0;
                        String shopProductId = null;
                        Set<String> shopProductIdList = new HashSet<>();
                        for (Dishes dishes : dishesList) {
                            if (!dishes.getBrief().contains("_")) {
                                continue;
                            }
                            if (dishes.getScore() > score) {
                                shopProductId = dishes.getBrief();
                                score = dishes.getScore();
                            }
                            shopProductIdList.add(dishes.getBrief());

                            logger.info("cutImgUrl:{},dishes.getBrief()={}, score={}", cutImgUrl, dishes.getBrief(), score);
                        }

                        logger.info("score=" + score);


                        int x1 = temp.getLocation().getLeft();
                        int y1 = temp.getLocation().getTop();

                        int x2 = temp.getLocation().getLeft() + temp.getLocation().getWidth();
                        int y2 = temp.getLocation().getTop() + temp.getLocation().getHeight();

                        ProductRecResultDetailVONewWithLocation productRecResultDetailVO
                                = new ProductRecResultDetailVONewWithLocation();

                        final Product[] product = new Product[1];

                        final Integer finalProductId;
                        if (shopProductId.contains("_")) {
                            finalProductId = Integer.valueOf(shopProductId.split("_")[1]);
                        } else {
                            finalProductId = null;
                        }
                        logger.info("cutImgUrl:{},shopProductId={}, finalProductId={}", cutImgUrl, shopProductId, finalProductId);

                        if (score < 0.55) {
                            if (recType == 1) {
                                logger.info("cutImgUrl:{}, score<0.8,score={}", cutImgUrl, score);
                                product[0] = noneRecProduct;
                                score = 0.2;
                                productRecResultDetailVO.setConfident(0);
                            }else{
                                logger.info("未识别菜品在不纠正界面不显示={}", cutImgUrl, score);
                                return null;
                            }
                        }else if (score < 0.75) {
                            logger.info("cutImgUrl:{}, score<0.8,score={}", cutImgUrl, score);
                            List<Integer> productIdList = new ArrayList<>();
                            shopProductIdList.stream().forEach(sp -> {
                                productIdList.add(Integer.valueOf(sp.split("_")[1]));
                            });

                            List<ProductRecOthersVO> productRecResultVOList = new ArrayList<>();

                            if (DataLoadService.productMap != null) {
                                for (Integer tempProductId : productIdList) {
                                    Product tempProduct = null;
                                    if (tempProductId == 1 || tempProductId < 0) {
                                        if (recType == 1) {
                                            tempProduct = noneRecProduct;
                                        }else {
                                            return null;
                                        }
                                    }else{
                                        tempProduct = DataLoadService.productMap.get(tempProductId);
                                        if (tempProduct == null) {
                                            tempProduct = productMapper.selectByPrimaryKey(tempProductId);
                                        }
                                    }
                                    ProductRecOthersVO recResultVO = new ProductRecOthersVO();
                                    recResultVO.setProductName(tempProduct.getName());
                                    recResultVO.setProductId(tempProduct.getId());
                                    recResultVO.setProductPrice(tempProduct.getPrice());
                                    recResultVO.setItemCount(1);

                                    productRecResultVOList.add(recResultVO);

                                    if (tempProduct.getId().equals(finalProductId)) {
                                        product[0] = tempProduct;
                                    }
                                }

                                productRecResultVOList.sort(Comparator.comparing(ProductRecOthersVO::getProductPrice));
                            }else{
                                String productIdListStr = org.apache.commons.lang3.StringUtils.join(productIdList.toArray(), ",");
                                List<Product> productList = productMapper.selectByIds(productIdListStr);



                                productList.stream().forEach(p -> {
                                    ProductRecOthersVO recResultVO = new ProductRecOthersVO();
                                    recResultVO.setProductName(p.getName());
                                    recResultVO.setProductPrice(p.getPrice());
                                    recResultVO.setProductId(p.getId());
                                    recResultVO.setItemCount(1);

                                    productRecResultVOList.add(recResultVO);

                                    if (p.getId().equals(finalProductId)) {
                                        product[0] = p;
                                    }
                                });
                            }

                            productRecResultDetailVO.setConfident(0);
                            productRecResultDetailVO.setOtherProductList(productRecResultVOList);
                        } else {
                            logger.info("cutImgUrl:{}, score>0.8,score={}", cutImgUrl, score);
                            productRecResultDetailVO.setConfident(1);
                            if (finalProductId < -1000) {
                                product[0] = productMapper.selectByPrimaryKey(Math.abs(finalProductId));
                            }else {
                                product[0] = productMapper.selectByPrimaryKey(finalProductId);
                            }

                            if (product[0] == null || product[0].getDeleted()) {
                                if (recType == 1) {
                                    product[0] = noneRecProduct;
                                    score = 0.2;
                                    productRecResultDetailVO.setConfident(0);
                                }else{
                                    return null;
                                }
                            }
                        }



                        logger.info("cutImgUrl:{}, product={}", cutImgUrl, JsonUtil.toJson(product[0]));
                        String label = product[0].getName();
//                            String label = product[0].getName() + "  " + PriceUtil.changeF2Y(product[0].getPrice()) + "元";

                        ImgTag imgTag = new ImgTag();
                        imgTag.setX1(x1);
                        imgTag.setY1(y1);
                        imgTag.setX2(x2);
                        imgTag.setY2(y2);
                        imgTag.setLabel(label);
//                        imgTag.setLabel(label);
                        if (score < 0.75) {
                            imgTag.setScore(0);
                        } else {
                            imgTag.setScore(1);
                        }
                        logger.info("==img:{}===rec result==imgTag={}=", cutImgUrl, JsonUtil.toJson(imgTag));

                        imgTagList.add(imgTag);

                        /**
                         * save ProductImgDetailModel
                         */
                        ProductImgDetailModel productImgDetailModel = new ProductImgDetailModel();
                        productImgDetailModel.setProductName(product[0].getName());
                        productImgDetailModel.setProductId(product[0].getId());
                        productImgDetailModel.setSeq(seq);
                        productImgDetailModel.setLeft(x1);
                        productImgDetailModel.setTop(y1);
                        productImgDetailModel.setWidth(temp.getLocation().getWidth());
                        productImgDetailModel.setHeight(temp.getLocation().getHeight());
                        productImgDetailModel.setScore(score);
                        productImgModel.getItemList().add(productImgDetailModel);
                        ///----end

                        productRecResultDetailVO.setX1(x1);
                        productRecResultDetailVO.setX2(x2);
                        productRecResultDetailVO.setY1(y1);
                        productRecResultDetailVO.setY2(y2);

                        productRecResultDetailVO.setProductName(product[0].getName());
                        productRecResultDetailVO.setProductId(product[0].getId());
                        productRecResultDetailVO.setProductPrice(product[0].getPrice());
                        productRecResultDetailVO.setItemCount(1);
                        productRecResultDetailVO.setSeq(seq);

                        productRecResultVO.getProductList().add(productRecResultDetailVO);
                        productRecResultVO.setTotalFee(productRecResultVO.getTotalFee().longValue()
                                + product[0].getPrice().longValue());

                        seq++;

                        long reRecEnd3 = System.currentTimeMillis();
                        logger.info("========cutImgPath:{}===识别时间{}====切图时间+识别时间{}==计算时间:{},总时间:{}" , cutImgPath, reRecEnd2-reRecEnd1, reRecEnd2-reRecStart, reRecEnd3-reRecEnd2, reRecEnd3 - reRecStart);
                        return seq;
                    }
                }
            }
        } catch (Exception e) {

            logger.info("===============end =====rec error:" + e.getMessage());
        }

        return null;
    }

    public void correctProductImg(ProductCorrectForm productCorrectForm) {

        if (productCorrectForm.getItemList() == null || productCorrectForm.getItemList().size() == 0) {
            return;
        }

        logger.info("===============start =====correctProductImg");
        try {
            ProductImgModel productImgModel = baiduRecUtil.getProductImgFromCache(productCorrectForm.getProductOrderNo());

            if (productImgModel == null) {
                return;
            }


            if (productImgModel.getItemList() != null && productImgModel.getItemList().size() > 0) {
                int i = 0;

                Map<Integer, ProductImgDetailModel> productImgDetailModelMap = new HashMap<>();

                Map<Integer, ProductCorrectItemForm> productCorrectItemFormHashMap = new HashMap<>();


                for (ProductCorrectItemForm productCorrectItemForm : productCorrectForm.getItemList()) {
                    productCorrectItemFormHashMap.put(productCorrectItemForm.getSeq(), productCorrectItemForm);
                }

                Map<Integer, Integer> productIdMap = new HashMap<>();
                for (ProductImgDetailModel productImgDetailModel : productImgModel.getItemList()) {
                    ProductCorrectItemForm productCorrectItemForm = productCorrectItemFormHashMap.get(productImgDetailModel.getSeq());

                    if (productCorrectItemForm != null) {
                        productIdMap.put(productCorrectItemForm.getProductId(), productImgDetailModel.getProductId());

                        productImgDetailModel.setProductId(productCorrectItemForm.getProductId());
                        productImgDetailModel.setProductName(productCorrectItemForm.getProductName());

                        productImgDetailModelMap.put(productImgDetailModel.getSeq(), productImgDetailModel);
                    }
                }

                String productImgModeStr = JsonUtil.toJson(productImgModel);
                logger.info(productImgModeStr);

                /**
                 * save to cache and db
                 */
                baiduRecUtil.setKey(productCorrectForm.getProductOrderNo(), productImgModel, 60 * 60 * 1000);
                ProductImgJsonInfo productImgJsonInfo = new ProductImgJsonInfo();
                productImgJsonInfo.setProductContent(productImgModeStr);

                Condition condition = new Condition(ProductImgJsonInfo.class);
                condition.or().andEqualTo("productOrderNo", productCorrectForm.getProductOrderNo());
                baiduRecUtil.productImgJsonInfoMapper.updateByConditionSelective(productImgJsonInfo, condition);
                //---end save---

                List<Integer> productIdList = null;
                if(DataLoadService.shopYpProductListMap != null){
                    List<ProductYpRef> temp = DataLoadService.shopYpProductListMap.get(productImgModel.getShopId());

                    if (temp != null && temp.size() > 0) {
                        productIdList = temp.stream().map(ProductYpRef::getProductId).collect(Collectors.toList());
                    }
                }


                for (ProductCorrectItemForm productCorrectItemForm : productCorrectForm.getItemList()) {
                    if (productIdList != null && productIdList.contains(productCorrectItemForm.getProductId())) {
                        logger.info("忽略云澎菜品库商品:" + productCorrectItemForm.getProductName());
                        continue;
                    }


                    ProductImgDetailModel productImgDetailModel = productImgDetailModelMap.get(productCorrectItemForm.getSeq());

                    Integer productId = productCorrectItemForm.getProductId();

                    if (productId != 1) {
                        /**
                         * 删除旧图片
                         */

                        logger.info("删除旧图片productId=" + productId);

                        ThreadManager.executorService.submit(() -> {
                            deleteProductImgCur(productId);
                        });
                    }


                    if (productImgDetailModel.getScore() > 0.75) {
                        Integer oldProductId = productIdMap.get(productId);
                        if (oldProductId != 1) {
                            logger.info("被修改的菜品分数大于0.75,删除旧图片oldProductId=" + oldProductId);
                            ThreadManager.executorService.submit(() -> {
                                deleteProductImgCur(oldProductId);
                            });
                        }
                    }


                    String localFilepath = "/usr/local/nginx/html/img/" + productImgModel.getShopId();

                    File shopFile = new File(localFilepath);
                    if (!shopFile.exists()) {
                        shopFile.mkdir();
                    }

                    localFilepath = localFilepath + "/" + productId;

                    File imgCutFile = new File(localFilepath);
                    if (!imgCutFile.exists()) {
                        imgCutFile.mkdir();
                    }
                    localFilepath += "/temp";

                    File imgCutFileTemp = new File(localFilepath);
                    if (!imgCutFileTemp.exists()) {
                        imgCutFileTemp.mkdir();
                    }

                    StringBuffer cutImgUrl = new StringBuffer("http://")
                            .append(ipAddress)
                            .append(":8019/img/")
                            .append(productImgModel.getShopId())
                            .append("/")
                            .append(productId)
                            .append("/temp/")
                            .append(productImgModel.getShopId())
                            .append("_")
                            .append(productId)
                            .append("_")
                            .append(productImgModel.getUuidFileName())
                            .append("cut")
                            .append(i)
                            .append(".png");

                    String cutImgPath = localFilepath + "/" + productImgModel.getShopId() + "_" + productId + "_" + productImgModel.getUuidFileName() + "cut" + i + ".png";
                    i++;

                    String sourceFilePath = productImgModel.getSourceFilename();
                    String sourceFilePathOnNet = productImgModel.getSourceImgOnInternet();
                    if (productImgModel.getCurIpAddress() != null &&
                            productImgModel.getCurIpAddress().equals(ipAddress)
                            ) {
                        LoggerUtil.console.info("source file ip : {}, sourceFilePath:", productImgModel.getCurIpAddress(), sourceFilePath);
                    } else {
                        LoggerUtil.console.info("source file ip : {}", productImgModel.getCurIpAddress());

                        String downloadNetFilepath = "/usr/local/nginx/html/img/" + productImgModel.getShopId() + "/" + productId + "/net";

                        File downloadNetFilepathFile = new File(downloadNetFilepath);
                        if (!downloadNetFilepathFile.exists()) {
                            downloadNetFilepathFile.mkdir();
                        }

                        int tempInt = new Random(100).nextInt();
                        downloadNetFilepath += "/" + productImgModel.getUuidFileName() + "." + tempInt + ".png";
                        ImageBase64Util.downloadNetImage(productImgModel.getSourceImgOnInternet(), downloadNetFilepath);

                        LoggerUtil.console.info("source file ip : {}, sourceFilePath:{}",
                                productImgModel.getCurIpAddress(), downloadNetFilepath);
                        sourceFilePath = downloadNetFilepath;

                        StringBuffer temp = new StringBuffer("http://")
                                .append(ipAddress)
                                .append(":8019/img/")
                                .append(productImgModel.getShopId())
                                .append("/")
                                .append(productId)
                                .append("/net/")
                                .append(productImgModel.getUuidFileName())
                                .append(".")
                                .append(tempInt)
                                .append(".png");

                        sourceFilePathOnNet = temp.toString();
                    }

                    int cut = 50;
                    ImageUtil.cutImageWithResize(sourceFilePath, cutImgPath
                            , productImgDetailModel.getLeft() + cut,
                            productImgDetailModel.getTop() + cut, productImgDetailModel.getWidth() - cut - 50, productImgDetailModel.getHeight() - cut - 50,350);

                    logger.info("===============cutImgPath==" + cutImgPath);

                    Integer branchId = null;
                    Product p = productMapper.selectByPrimaryKey(productId);
                    if (p != null) {
                        branchId = p.getStoreid();
                    }

                    List<LogProductImgFromUser> productImgList = baiduRecUtil.productService.getProductImgFromUserListFromCorrect(productId);

                    if (productImgList != null && productImgList.size() > 10) {
                        LogProductImgFromUser correctedImg = productImgList.get(0);
                        ypRecService.delImg(correctedImg.getImgCutNetUrl(), branchId, this.localIpAddress + ":5001");
//                        ypRecService.delImg(correctedImg.getImgCutNetUrl(), productImgModel.getShopId(), this.localIpAddress + ":5001");

                    }

//                    ypRecService.addImg(cutImgUrl.toString(), productImgModel.getShopId(), this.localIpAddress + ":5001");
                    ypRecService.addImg(cutImgUrl.toString(), branchId,  productId,this.localIpAddress + ":5001");

                    LogProductImgFromUser logProductImgFromUser = new LogProductImgFromUser();
                    logProductImgFromUser.setCreateTime(new Date());
                    logProductImgFromUser.setCutImg(cutImgPath);
                    logProductImgFromUser.setSourceImg(productImgModel.getSourceFilename());
                    logProductImgFromUser.setProductId(productId);
                    logProductImgFromUser.setShopId(productImgModel.getShopId());
                    logProductImgFromUser.setFromCorrect(1);
                    logProductImgFromUser.setBranchId(branchId);
                    logProductImgFromUser.setBaiduDeleted(0);
                    logProductImgFromUser.setImgCutNetUrl(cutImgUrl.toString());
                    logProductImgFromUser.setImgSrcNetUrl(sourceFilePathOnNet);
                    logProductImgFromUser.setSubDb(0);


                    baiduRecUtil.logProductImgFromUserMapper.insert(logProductImgFromUser);

                }
            }

            logger.info("===============end=====correctProductImg");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    public void deleteProductImgCur(Integer productId){
        /*List<LogProductImg> logProductImgList = productService.getProductImgList(productId);
        if (logProductImgList != null) {
            List<Integer> productImgIdList = new ArrayList<>();

            logProductImgList.stream().forEach(p -> {
                if(delImg(p.getContSign(), p.getShopId())){
                    productImgIdList.add(p.getId());
                }
            });

            if (productImgIdList.size() > 0) {
                productService.deleteProductImgList(productImgIdList);
            }
        }*/

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -2);

        List<LogProductImgFromUser> logProductImgFromUserList = baiduRecUtil.productService.getProductImgFromUserList(productId);
        if (logProductImgFromUserList != null) {
            List<Integer> productImgIdList = new ArrayList<>();
            logProductImgFromUserList.stream().forEach(p -> {
                if (p.getFromCorrect() != null && p.getFromCorrect().intValue() == 1) {

                    if (p.getCreateTime().after(calendar.getTime())) {
                        logger.info("一周内的纠正不删除:" + p.getId() + ", productId:" + p.getProductId());
                    }else {
                        logger.info("删除:" + p.getContSign() + ", productId:" + p.getProductId());
                        Integer branchId = getBranchIdByShopId(p.getShopId(), p.getBranchId(), p.getProductId());
                        ypRecService.delImg(p.getImgCutNetUrl(), branchId, this.localIpAddress + ":5001");
//                        ypRecService.delImg(p.getImgCutNetUrl(), p.getShopId(), this.localIpAddress + ":5001");
                        productImgIdList.add(p.getId());
                    }
                }else {
                    logger.info("删除:" + p.getContSign() + ", productId:" + p.getProductId());
                    Integer branchId = getBranchIdByShopId(p.getShopId(), p.getBranchId(), p.getProductId());
                    ypRecService.delImg(p.getImgCutNetUrl(), branchId, this.localIpAddress + ":5001");
//                    ypRecService.delImg(p.getImgCutNetUrl(), p.getShopId(), this.localIpAddress + ":5001");
                    productImgIdList.add(p.getId());
                }
            });

            if (productImgIdList.size() > 0) {
                baiduRecUtil.productService.deleteProductImgFromUserList(productImgIdList);
            }
        }

    }


    private Integer getBranchIdByShopId(Integer shopId, Integer branchId, Integer productId) {
        if (branchId != null && branchId > 0) {
            return branchId;
        }

        if (shopId != null && shopId > 0) {
            Condition condition = new Condition(Branch.class);
            condition.or().andEqualTo("shopId", shopId);
            List<Branch> branchList = branchMapper.selectByCondition(condition);
            if (branchList != null && branchList.size() > 0) {
                if (branchList.size() > 1) {
                    return branchList.get(0).getBranchId();
                }

                return branchList.get(0).getBranchId();
            }else {
                return null;
            }
        }

        if (productId != null && productId > 0) {
            Product product = productMapper.selectByPrimaryKey(productId);
            return product.getStoreid();
        }

        return null;
    }

}
